﻿using System.Text;
using Azure.Online.Proxy.Server;

namespace Azure.Online.Proxy.Actions {
    public class OptionsActionResult : ActionResult {
        private readonly string[] _allowedHeaders = new[] {
            "accept",
            "Authorization",
            "Content-Type",
            "Content-Length",
            "If-Match",
            "dataserviceversion",
            "maxdataserviceversion",
            "x-ms-blob-content-length",
            "x-ms-blob-type",
            "x-ms-date", 
            "x-ms-lease-action",
            "x-ms-lease-id",
            "x-ms-lease-duration",
            "x-ms-range",
            "x-ms-version",
            "x-azure-online-accept-base64",
            "x-azure-online-content-length",
            "x-azure-online-content-type",
            "x-azure-online-date",
            "x-azure-online-url",
            "x-azure-online-meta"
        };

        public override void Execute(HttpServerContext context) {
            context.Response.StatusCode = 200;
            context.Response.StatusDescription = "OK";
            context.Response.Headers["Access-Control-Allow-Origin"] = "*";
            context.Response.Headers["Access-Control-Allow-Methods"] = "OPTIONS, GET, POST, PUT, DELETE";
            context.Response.Headers["Allow"] = "OPTIONS, GET, POST, PUT, DELETE";
            context.Response.Headers["Access-Control-Allow-Headers"] = string.Join(",", _allowedHeaders);

            var response = Encoding.UTF8.GetBytes("");
            context.Response.ContentType = "text/plain";
            context.Response.ContentLength = response.Length;

            context.Response.OutputStream.Write(response, 0, response.Length);
            context.Response.OutputStream.Flush();
            context.Response.OutputStream.Close();
        }
    }
}
